#include "russiatetrix.h"

RussiaTetrix::RussiaTetrix()
{
    _posX = _posY = _remove_line = 0;
    _status = STOP;
    _container.clean();
    _piece.clean();
}

RussiaTetrix::RussiaTetrix(quint8 row,quint8 col)
    : _container(row,col)
{
    _status = STOP;
    _posX = _posY = _remove_line = 0;
    _piece.clean();
}

bool RussiaTetrix::moveLeft()
{
    if(_posX <= 0 || isCollision(Left))
        return false;

    _posX--;
    return true;
}

bool RussiaTetrix::moveRight()
{
    if(_posX + _piece.width() >= _container.width() || isCollision(Right))
        return false;

    _posX++;
    return true;
}

bool RussiaTetrix::dropDown()
{
    if(isCollision(Down))
        return false;

    _posY++;
    return true;
}

bool RussiaTetrix::rotateLeft()
{
    if(isCollision(Down))
        return false;

    ToyBricks tb = _piece;
    _piece.rotateLeft();

    //provent piece from surpass boarder
    quint8 old_posX = _posX;
    if(_posX + _piece.width() > _container.width())
        _posX = _container.width() - _piece.width();

    if(isCollision(Here)){
        _piece = tb;
        _posX = old_posX;
        return false;
    }

    return true;
}

quint8 RussiaTetrix::removeCompletedLine()
{
    quint8 cnt = 0;
    for(qint8 row = _container.height() - 1; row >= 0; row--){
        if(_container.getRowFilledCnt(row) == _container.width()/*fill_cnt == _container.width()*/){//remove completed line
            this->_remove_line++;
            cnt++;

            //move data block
            for(qint32 idx = row * _container.width() - 1; idx >= 0; idx--){
                _container.setValue(idx + _container.width(),_container.getValue(idx));
            }

            //set line zero's value to 0
            for(quint8 idx = 0; idx < _container.width(); idx++)
                _container.setValue(idx,0);

            //move line flag
            for(quint8 idx = row;idx > 0; idx--)
                _container.setRowFilledCnt(idx,_container.getRowFilledCnt(idx - 1));

            //set line zero's flag to false
            _container.setRowFilledCnt(0,0);
        }
    }
    return cnt;
}

void RussiaTetrix::draw(QPainter &painter,const QPoint &point)
{
    QPen pen(Qt::DashLine);
    pen.setColor(Qt::gray);
    painter.setPen(pen);

    for(quint8 i = 0; i <= _container.height(); i++)    //draw horizontal line
        painter.drawLine(point.x(),RUSSIA_TETRIX_SIZE * i + point.y(),point.x() + _container.width() * RUSSIA_TETRIX_SIZE,RUSSIA_TETRIX_SIZE * i  + point.y());

    for(quint8 i = 0; i <= _container.width(); i++) //draw vertical line
        painter.drawLine(point.x() + RUSSIA_TETRIX_SIZE * i,point.y(),point.x() + RUSSIA_TETRIX_SIZE * i,(_container.height() + 1) * RUSSIA_TETRIX_SIZE);

    //draw container
    if(_container.size() > 0)
        _container.draw(painter,point,RUSSIA_TETRIX_SIZE);
    //draw piece
    if(_piece.size() > 0)
        _piece.draw(painter,point + QPoint(_posX * RUSSIA_TETRIX_SIZE,_posY * RUSSIA_TETRIX_SIZE),RUSSIA_TETRIX_SIZE);
}

bool RussiaTetrix::isCollision(MoveDirection direction)
{
    bool result = false;
    if(_piece.height() + _posY >= _container.height())
        result = true;
    else
    {
        if(direction == Left)
            result = _container.isCollision(_piece,QPoint(_posX - 1,_posY));
        else if(direction == Right)
            result = _container.isCollision(_piece,QPoint(_posX + 1,_posY));
        else if(direction == Down)
            result = _container.isCollision(_piece,QPoint(_posX,_posY + 1));
        else
            result = _container.isCollision(_piece,QPoint(_posX,_posY));
    }

    if(result && this->_posY < 0){
        this->_status = GAMEOVER;
    }

    return result;
}

void RussiaTetrix::handelCollision()
{
    if(_piece.size())
        _container.join(_piece,QPoint(X(),Y()));

    this->changePiece();
}


